﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

using ProjectBase.Core;
using ProjectBase.Core.Service;
using ProjectBase.Core.Web;

namespace $safeprojectname$.Models
{
    /// <summary>
    ///     Base class for the standard Entities with int ID
    /// </summary>
    /// <typeparam name = "T">Entity with int ID to be moved between Controller and View</typeparam>
    /// <typeparam name = "TFilter"></typeparam>
    public abstract class EntityModel<T, TFilter> : EntityModel<T, int, TFilter>
        where T : class, IPersistentObjectWithTypedId<int>
        where TFilter : class, IFilter<int> { }

    /// <summary>
    ///     Base model for every entity of type T
    ///     containing all needed properties to fill MasterPage
    ///     and to support EntityController actions
    /// </summary>
    /// <typeparam name = "T">Entity with int ID to be moved between Controller and View</typeparam>
    /// <typeparam name = "TID">type of the ID (string is not supported in the base EntityController implemenatation)</typeparam>
    /// <typeparam name = "TFilter"></typeparam>
    public abstract class EntityModel<T, TID, TFilter> : WebModel, IEditModel<T, TID, TFilter>
        where T : class, IPersistentObjectWithTypedId<TID>
        where TFilter : class, IFilter<TID>
    {
        #region members
        IListModel _listModel;
        #endregion members

        #region T, TFilter
        /// <summary>
        ///     Current Entity Item used on Detail, New and Edit
        /// </summary>
        public virtual T Item { get; set; }

        /// <summary>
        ///     Filter object for Entity Item
        /// </summary>
        public virtual TFilter Filter { get; set; }
        #endregion

        #region Models

        /// <summary>
        ///     Data source for List view on current entity.
        /// </summary>
        public virtual IListModel ListModel
        {
            get
            {
                if (_listModel.IsNull())
                {
                    _listModel = Factory.CreateInstance<IListModel>();
                }
                if (_listModel.Filter.IsNull())
                {
                    _listModel.SetFilter(() => Filter);
                    _listModel.SetCurrentController(() => MasterModel.ControllerName);
                    _listModel.SetCurrentAction(() => MasterModel.CurrentAction);
                }
                return _listModel;
            }
            set { _listModel = value; }
        }
        #endregion Models

    }
}